package com.quick.develop.flowable.framework.cmd;

import com.quick.develop.flowable.constant.BpmConstants;
import com.quick.develop.framework.utils.uuid.IdUtils;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.cmd.NeedsActiveTaskCmd;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;

import java.util.Objects;

/**
 * @author junyuan.liu
 * @description: 创建子任务，ru_task、hi_task
 *                  
 * @date 2022/8/18 13:10
 */
public class CreateSubTaskCmd extends NeedsActiveTaskCmd<TaskEntity> {
    private static final long serialVersionUID = 5177251319728119891L;
    
    protected String assignee;
    protected String category;
    protected String description;

    public CreateSubTaskCmd(String taskId, String assignee, String category, String description) {
        super(taskId);
        this.assignee = assignee;
        this.category = category;
        this.description = description;
    }

    @Override
    protected TaskEntity execute(CommandContext commandContext, TaskEntity taskEntity) {
        TaskService taskService = CommandContextUtil.getProcessEngineConfiguration(commandContext).getTaskService();
        TaskEntity subTask = buildSubTask(commandContext, taskService, taskEntity);
        taskService.saveTask(subTask);

        return subTask;
    }
    
    
    protected TaskEntity buildSubTask(CommandContext commandContext, TaskService taskService, TaskEntity task) {
        TaskEntity subTask = (TaskEntity) taskService.newTask(IdUtils.randomUUID());
        subTask.setAssignee(assignee);
        subTask.setProcessInstanceId(task.getProcessInstanceId());
        subTask.setProcessDefinitionId(task.getProcessDefinitionId());
        subTask.setName(task.getName());
        subTask.setParentTaskId(task.getId());
        subTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
        subTask.setTaskDefinitionId(task.getTaskDefinitionId());
        subTask.setExecutionId(task.getExecutionId());
        subTask.setPriority(task.getPriority());
        subTask.setCreateTime(CommandContextUtil.getProcessEngineConfiguration(commandContext).getClock().getCurrentTime());
        subTask.setSuspensionState(task.getSuspensionState());
        subTask.setTenantId(task.getTenantId());
        
        if (Objects.nonNull(category)) {
            subTask.setCategory(category);
        }else {
            subTask.setCategory(task.getCategory());
        }
        if (Objects.nonNull(description)) {
            if (Objects.nonNull(task.getDescription())) {
                subTask.setDescription(task.getDescription() + BpmConstants.COMMENT_SEPARATOR + description);
            }else {
                subTask.setDescription(description);
            }
        }else {
            subTask.setDescription(task.getDescription());
        }
        
        return subTask;
    }
    
    
}
